package com.vedioview.demo;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import org.w3c.dom.Text;

public class MainActivity extends Activity {


    /**
     * Called when the activity is first created.
     */
    private VideoView videoView;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    MediaController mMediaCtrl;
    private String url1="http://wy.test.uyuan.info/resources/video/test.mp4";
    private String url2 = "http://ht-mobile.cdn.turner.com/nba/big/teams/kings/2014/12/12/HollinsGlassmov-3462827_8382664.mp4";
    private String url3 = "http://ht-mobile.cdn.turner.com/nba/big/teams/kings/2014/12/12/VivekSilverIndiamov-3462702_8380205.mp4";
    private boolean isPaused = false;
    private boolean isPlaying = false;
    private TextView videoMessage;

    /**
     * 当前视频播放的状态值
     */
    private static int playState = MainActivity.STATE_DEFAULT;
    /**
     * 视频准备之前的初始化过程状态
     */
    public final static int STATE_DEFAULT = 0;
    /**
     * 视频准备完成
     */
    public final static int STATE_PREPARED = 1;
    /**
     * 视频播放结束
     */
    public final static int STATE_COMPLETED = 2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        videoView = (VideoView) findViewById(R.id.video_view);
        videoMessage = (TextView) findViewById(R.id.videoMessage);
        init();
        btn1 = (Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "等等啦", Toast.LENGTH_SHORT).show();
                //之前播放过，无需prepare的情形下，设置相关
                if (playState == MainActivity.STATE_COMPLETED) {
                    videoMessage.setVisibility(View.GONE);
                    videoView.setBackgroundColor(Color.parseColor("#00000000"));
                }
                videoView.start();

            }
        });
        btn2 = (Button) findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "暂停了", Toast.LENGTH_SHORT).show();
                videoView.pause();
            }
        });
        btn3 = (Button) findViewById(R.id.btn3);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Toast.makeText(MainActivity.this, "等等啦", Toast.LENGTH_SHORT).show();
//                if (videoView.isPlaying()) {
//                    videoView.resume();
//                }
                if (playState == MainActivity.STATE_COMPLETED) {
                    videoMessage.setVisibility(View.GONE);
                    videoView.setBackgroundColor(Color.parseColor("#00000000"));
                }
                videoView.setVideoPath(url3);
                videoView.start();
            }
        });
    }

    private void init() {
        mMediaCtrl = new MediaController(this, false);
        mMediaCtrl.setAnchorView(videoView);
        mMediaCtrl.setMediaPlayer(videoView);
        mMediaCtrl.setPadding(0, 0, 0, 100);
        videoView.setMediaController(null);
        videoView.requestFocus();
        videoView.setVideoPath(url2);
        videoView.start();
        //视频播放完成以后，被重新播放，onPrepared方法，不会被再次回调
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                Log.e("videoview", "onPrepared");
                videoView.setBackgroundColor(Color.parseColor("#00000000"));
                videoMessage.setText("正在加载...");
                videoMessage.setVisibility(View.GONE);
                playState = MainActivity.STATE_PREPARED;
                mMediaCtrl.hide();
                handler.postDelayed(runnable, 0);
            }
        });
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                //关闭runable
                handler.removeCallbacks(runnable);
                videoMessage.setText("心动了吧，那就约起来...");
                videoView.setBackgroundResource(R.mipmap.bg);
                videoMessage.setVisibility(View.VISIBLE);
                playState = MainActivity.STATE_COMPLETED;
            }
        });


        //添加本地的文件，播放本地视频
//        File file=new File(Environment.getExternalStorageDirectory(),"mm.3gp");
//        videoView.setVideoPath(file.getPath());

        // 播放网络视频


    }

    ////////////////////////此处处理网络不好的时候，显示正在加载的状态////////////////
    int old_duration = 0;
    final Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        public void run() {
            int duration = videoView.getCurrentPosition();
            if (old_duration == duration && videoView.isPlaying()) {
                videoMessage.setVisibility(View.VISIBLE);
            } else {
                videoMessage.setVisibility(View.GONE);
            }
            old_duration = duration;

            handler.postDelayed(runnable, 1000);
        }
    };
    ////////////////////////////////////////////////////////////////////////////////

}
